﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.Office.Tools.Excel;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Mathos;

namespace BHA
{
    public partial class DBEquip
    {
        public List<ItemSAP> DatabaseEquip;
        public List<PerfType> DatabasePerfType;

        private void DBEquip_Startup(object sender, System.EventArgs e)
        {
            DatabaseEquip = new List<ItemSAP>();
            DatabasePerfType = new List<PerfType>();
            Excel.Range saprange = Application.Intersect(UsedRange, DBEquip_SAP.RefersToRange);
            ItemSAP itemsap = null; PerfType perftype = null;
            foreach (Excel.Range sapcell in saprange)
            {
                if (ItemSAP.IsExtSAP(sapcell.Text))
                {   // Sap Cell Found, Init item
                    if (perftype != null) { DatabasePerfType.Add(perftype.Clone()); perftype = null; }
                    if (itemsap != null) DatabaseEquip.Add(itemsap);
                    itemsap = new ItemSAP();
                    itemsap.NoSAP = ItemSAP.GetExtSAP(sapcell.Text);
                    itemsap.DescriptionUSE = sapcell.Offset[0, 1].Text;
                }
                else if (PerfType.IsPerfType(sapcell.Text))
                {   // Perf Type Found, Init item
                    if (itemsap != null) { DatabaseEquip.Add(itemsap.Clone(1)); itemsap = null; }
                    if (perftype != null) DatabasePerfType.Add(perftype);
                    perftype = new PerfType();
                    perftype.CODE = sapcell.Text;
                    perftype.Description = sapcell.Offset[0, 1].Text;
                }
                // else if ItemSAP is PERFTYPE SAP
                else
                {   // If Sap Cell or PERFTYPE Found Already
                    if (itemsap != null)
                    {   // Already Init, now put Sub Item
                        if (ItemSAP.IsExtSAP(sapcell.Offset[0, 1].Text))
                        {   // Sub Item Found
                            ItemSAP subitem = new ItemSAP();
                            subitem.NoSAP = ItemSAP.GetExtSAP(sapcell.Offset[0, 1].Text);
                            subitem.DescriptionUSE = sapcell.Offset[0, 2].Text;
                            subitem.TrySetQuantity(sapcell.Offset[0, 3].Text);
                            itemsap.Items.Add(subitem);
                        }
                        //else if (3above or 0below)
                    }
                    else if (perftype != null)
                    {
                        if (ItemSAP.IsExtSAP(sapcell.Offset[0, 1].Text))
                        {
                            ItemSAP subitem = new ItemSAP();
                            subitem.NoSAP = ItemSAP.GetExtSAP(sapcell.Offset[0, 1].Text);
                            subitem.DescriptionUSE = sapcell.Offset[0, 2].Text;
                            //subitem.TrySetQuantity(sapcell.Offset[0, 3].Text);
                            string s = sapcell.Offset[0, 3].Text;
                            if (string.IsNullOrWhiteSpace(s)) { MessageBox.Show("Quantity Formula is EMPTY at " + sapcell.Offset[0, 3].Address); continue; }
                            else subitem.QuanFormula = sapcell.Offset[0, 3].Text;
                            perftype.Items.Add(subitem);
                        }
                    }
                }
            }
            if (itemsap != null) DatabaseEquip.Add(itemsap);
            if (perftype != null) DatabasePerfType.Add(perftype);
        }

        private void DBEquip_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(this.DBEquip_Startup);
            this.Shutdown += new System.EventHandler(this.DBEquip_Shutdown);

        }

        #endregion
    }
}
